//
//  NetManager.m
//  Net2
//
//  Created by student on 16/3/29.
//  Copyright © 2016年 NIIT. All rights reserved.
//

#import "NetManager.h"
#import "def.h"
@implementation NetManager

SingletonM(NetManager)

- (void)loginWithUserName:(NSString *)username
              andPassword:(NSString *)password
{
    NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",username,password];
    
    [self requestInfoWithURLStr:str successBlock:^(NSData *data)
    {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
        if(dict[@"error"] != nil)
        {
            NSError *error = [NSError errorWithDomain:@"com.niit.Net2" code:1 userInfo:@{NSLocalizedDescriptionKey:dict[@"error"]}];
            [self.delegate loginFail:error];
        }
        else
        {
            [self.delegate loginSuccess];
        }
    }
        faileBlock:^(NSError *error)
     {
         [self.delegate loginFail:error];
     }];
}


- (void)requestInfoWithURLStr:(NSString *)urlStr
                   successBlock:(void (^)(NSData *))successBlock
                     faileBlock:(void (^)(NSError *))failBlock
    {
        // 请求网址
        NSURL *url = [NSURL URLWithString:urlStr];
        // 请求
        NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
        // 发送异步请求
        [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
            if(connectionError == nil)
            {
                // 请求成功
                successBlock(data);
            }
            else
            {
                // 请求失败
                failBlock(connectionError);
            }
        }];
    }
     
- (void)requestVideosList
{
    NSString *str = @"http://localhost:8080/MJServer/video?method=get&type=JSON";
    NSURL *url = [NSURL URLWithString:str];
    NSURLRequest *urlrequest = [NSURLRequest requestWithURL:url];
    
    [NSURLConnection sendAsynchronousRequest:urlrequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError)
     {
        if (connectionError == nil)
        {
            [self.delegate requestMovieListSuccess];
        }
        else
        {
            [self.delegate requestMovieListFail];
        }
     }];
}

//+ (BOOL)loginWithUserName:(NSString *)username andPassword:(NSString *)password
//{
//    NSString *str = [NSString stringWithFormat:@"http://localhost:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",username,password];
//    // 请求网址
//    NSURL *url = [NSURL URLWithString:str];
//    // 请求
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//    
//    NSData *data = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:nil];
//    
//    NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//    NSLog(@"%@",result);
//    
//    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//    
//    if (dict[@"error"] != nil)
//    {
//        return NO;
//    }
//    else
//    {
//        return YES;
//    }
//}
//
//+ (void)loginWithUserName:(NSString *)username andPassword:(NSString *)password successBlock:(void(^)())successBlock failedBlock:(void(^)(NSError *))failBlock
//{
//    NSString *str = [NSString stringWithFormat:@"http://192.168.13.28:8080/MJServer/login?username=%@&pwd=%@&method=get&type=JSON",username,password];
//    // 请求网址
//    NSURL *url = [NSURL URLWithString:str];
//    // 请求
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//    
//    // 发送异步请求
//    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        
//        if(connectionError == nil)
//        {
//            NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"%@",result);
//            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
//            
//            //{"success":"登录成功"}
//            //{"error":"密码不正确"}
//            if(dict[@"error"] != nil)
//            {
//                
//                // 定义一个NSError对象
//                NSError *error = [NSError errorWithDomain:@"com.niit.net2Demo"
//                                                     code:kErrorLogin               // 错误编码
//                                                 userInfo:@{NSLocalizedDescriptionKey:dict[@"error"]}];
//                // 已修正 @"NSLocalizedDescriptionKey" => NSLocalizedDescriptionKey
//                
//                // 执行失败时要执行block代码
//                failBlock(error);
//            }
//            else
//            {
//                // 成功时的block
//                successBlock();
//            }
//            
//        }
//        else
//        {
//            failBlock(connectionError);
//        }
//        
//    }];
//}
//
//+ (void)requestInfoWithURLStr:(NSString *)urlStr
//                 successBlock:(void (^)(NSData *))successBlock
//                   faileBlock:(void (^)(NSError *))failBlock
//{
//    // 请求网址
//    NSURL *url = [NSURL URLWithString:urlStr];
//    // 请求
//    NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
//    // 发送异步请求
//    [NSURLConnection sendAsynchronousRequest:urlRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
//        if(connectionError == nil)
//        {
//            // 请求成功
//            successBlock(data);
//        }
//        else
//        {
//            // 请求失败
//            failBlock(connectionError);
//        }
//    }];
//}
//
@end
